#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int g[9][9], w[9][9];
int row_hash[9], col_hash[9], block_hash[9];
vector<pair<int, int>> zero_pos;
bool used[81] = {false};
int ans = -1;
vector<int> num_list[1024];

int ToBlockId(int x, int y) {
  return (x / 3) * 3 + y / 3;
}

void ChangeHash(int x, int y) {
  row_hash[x] ^= 1 << g[x][y];
  col_hash[y] ^= 1 << g[x][y];
  block_hash[ToBlockId(x, y)] ^= 1 << g[x][y];
}

void CalcNumList() {
  for (int i = 0; i < 1024; i++) {
    for (int j = 0; j < 9; j++) {
      if ((i & (1 << j)) == 0) {
        num_list[i].push_back(j + 1);
      }
    }
  }
}

const vector<int> &GetNumList(int x, int y) {
  return num_list[(row_hash[x] | col_hash[y] | block_hash[ToBlockId(x, y)]) / 2];
}

int GetNumCount(int x, int y) {
  return num_list[(row_hash[x] | col_hash[y] | block_hash[ToBlockId(x, y)]) / 2].size();
}

void DFS(int dep) {
  if (dep == zero_pos.size()) {
    int tmp = 0;
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        tmp += g[i][j] * w[i][j];
      }
    }
    ans = max(ans, tmp);
    return;
  }
  int min_pos = -1, min_num = 10;

  for (int i = 0; i < zero_pos.size(); i++) {
    if (used[i] == false) {
      int nc = GetNumCount(zero_pos[i].first, zero_pos[i].second);

      if (nc < min_num) {
        min_num = nc;
        min_pos = i;
      }
    }
  }

  used[min_pos] = true;
  int x = zero_pos[min_pos].first, y = zero_pos[min_pos].second;
  for (int num : GetNumList(x, y)) {
    g[x][y] = num;
    ChangeHash(x, y);
    DFS(dep + 1);
    ChangeHash(x, y);
    g[x][y] = 0;
  }
  used[min_pos] = false;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      cin >> g[i][j];
      w[i][j] = 10 - max(abs(i - 4), abs(j - 4));
      if (g[i][j]) {
        ChangeHash(i, j);
      } else {
        zero_pos.emplace_back(make_pair(i, j));
      }
    }
  }
  CalcNumList();
  DFS(0);
  cout << ans << endl;
  return 0;
}
